/*
 * SimDelayDialog.java
 *
 * Created on April 12, 2005, 11:20 AM
 */

package gui;

import javax.swing.JDialog;

/**
 * A dialog box for editing the signal propagation delay.
 *
 * @author
 * @author  Kevin Wellwood
 */
public class SimDelayDialog extends JDialog {
    
    /**
     * Creates new SimDelayDialog.
     *
     * @param gui the gui
     */
    public SimDelayDialog(Gui gui) {
        super(gui);
        initComponents();
        delayField.setText("" + gui.getSimDelay());
        newDelay = gui.getSimDelay();
    }
    
    /**
     * Displays the dialog for the user.
     *
     * @return the user's response- the simulation delay in milliseconds
     */
    public long showDialog() {
        setVisible(true);
        return newDelay;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;
        javax.swing.JButton jButton1;
        javax.swing.JButton jButton2;
        javax.swing.JLabel jLabel1;

        jLabel1 = new javax.swing.JLabel();
        delayField = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Simulation Speed");
        setModal(true);
        setResizable(false);
        jLabel1.setText("Enter the signal propagation delay (ms):");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        getContentPane().add(jLabel1, gridBagConstraints);

        delayField.setMinimumSize(new java.awt.Dimension(60, 20));
        delayField.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 3, 0, 0);
        getContentPane().add(delayField, gridBagConstraints);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonPressed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 3);
        getContentPane().add(jButton1, gridBagConstraints);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonPressed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 0, 0);
        getContentPane().add(jButton2, gridBagConstraints);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }//GEN-END:initComponents

    private void cancelButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonPressed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonPressed

    private void okButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonPressed
        try {
            newDelay = Integer.parseInt(delayField.getText());
            setVisible(false);
        } catch (NumberFormatException e) {
            /* do nothing */
        }
    }//GEN-LAST:event_okButtonPressed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField delayField;
    // End of variables declaration//GEN-END:variables
    /** the gui */
    private Gui gui;
    /** the delay to be returned to the user (ms) */
    private long newDelay;
}
